const Joi = require('joi');
const validationMiddleware = require('../../middleware/validate');
const router = require('koa-router')();
const { dealNotEmpty, dealNotEmptyArry } = require("../../utils/dberrordeal");
const RoleController = require('../../controllers/system/roleController');

const rulesSchema = {
    '/addrole': Joi.object({
        name: dealNotEmpty('名称'),
        roles: dealNotEmptyArry('角色权限')
    }),
    '/putrole': Joi.object({
        id: dealNotEmpty('id'),
        name: dealNotEmpty('名称'),
        roles: dealNotEmptyArry('角色权限')
    }),
    '/delrole': Joi.object({
        id: dealNotEmpty('id')
    }),
    '/getrolei': Joi.object({
        id: dealNotEmpty('id')
    })
};

router.post('/addrole', validationMiddleware(rulesSchema), RoleController.addRole);
router.post('/putrole', validationMiddleware(rulesSchema), RoleController.updateRole);
router.post('/getrolei', validationMiddleware(rulesSchema), RoleController.getRoleById);
router.post('/rolelist', RoleController.getRoleList);
router.post('/delrole', validationMiddleware(rulesSchema), RoleController.deleteRole);

module.exports = router;

/**
 * @swagger
 * tags:
 *   name: 角色管理
 *   description: 角色权限相关接口
 */

/**
 * @swagger
 * components:
 *   schemas:
 *     Role:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *           example: 507f1f77bcf86cd799439011
 *         name:
 *           type: string
 *           example: 管理员
 *         roles:
 *           type: array
 *           items:
 *             type: string
 *           example: ["507f1f77bcf86cd799439012"]
 *     RoleDetail:
 *       type: object
 *       properties:
 *         _id:
 *           type: string
 *         name:
 *           type: string
 *         roles:
 *           type: array
 *           items:
 *             type: object
 *             properties:
 *               name:
 *                 type: string
 *               path:
 *                 type: string
 *               isMenu:
 *                 type: boolean
 *               order:
 *                 type: number
 *               parentId:
 *                 type: string
 *               alias:
 *                 type: string
 */

/**
 * @swagger
 * /addrole:
 *   post:
 *     tags: [角色管理]
 *     summary: 添加角色
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - name
 *               - roles
 *             properties:
 *               name:
 *                 type: string
 *                 example: 管理员
 *               roles:
 *                 type: array
 *                 items:
 *                   type: string
 *                 example: ["507f1f77bcf86cd799439012"]
 *     responses:
 *       200:
 *         description: 添加成功
 *       400:
 *         description: 添加失败
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 message:
 *                   type: string
 *                   example: 角色名称已存在
 */

/**
 * @swagger
 * /putrole:
 *   post:
 *     tags: [角色管理]
 *     summary: 更新角色
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *               - name
 *               - roles
 *             properties:
 *               id:
 *                 type: string
 *                 example: 507f1f77bcf86cd799439011
 *               name:
 *                 type: string
 *                 example: 管理员
 *               roles:
 *                 type: array
 *                 items:
 *                   type: string
 *                 example: ["507f1f77bcf86cd799439012"]
 *     responses:
 *       200:
 *         description: 更新成功
 *       400:
 *         description: 更新失败
 */

/**
 * @swagger
 * /getrolei:
 *   post:
 *     tags: [角色管理]
 *     summary: 获取角色详情
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *             properties:
 *               id:
 *                 type: string
 *                 example: 507f1f77bcf86cd799439011
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/RoleDetail'
 *       400:
 *         description: 获取失败
 */

/**
 * @swagger
 * /rolelist:
 *   get:
 *     tags: [角色管理]
 *     summary: 获取角色列表
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               type: object
 *               properties:
 *                 data:
 *                   type: array
 *                   items:
 *                     $ref: '#/components/schemas/RoleDetail'
 *       400:
 *         description: 获取失败
 */

/**
 * @swagger
 * /delrole:
 *   post:
 *     tags: [角色管理]
 *     summary: 删除角色
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             required:
 *               - id
 *             properties:
 *               id:
 *                 type: string
 *                 example: 507f1f77bcf86cd799439011
 *     responses:
 *       200:
 *         description: 删除成功
 *       400:
 *         description: 删除失败
 */